/**
 * 资源管理的单例
 */
var Resource = {
    id: "resourceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Resource.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '资源名称', field: 'name', align: 'center', valign: 'middle', sortable: true,width:'20%'},
        {title: '资源权限', field: 'perms', align: 'center', valign: 'middle', sortable: true,width:'15%'},
        {title: '请求地址', field: 'url', align: 'center', valign: 'middle', sortable: true,width:'20%'},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true,width:'8%'},
        {title: '层级', field: 'levels', align: 'center', valign: 'middle', sortable: true,width:'8%'},
        {title: '资源类型', field: 'resourceTypeName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='菜单'){
                return "<span class='label label-warning'>菜单</span>";
        	}else if(value=='目录'){
                return "<span class='label label-success'>目录</span>";
        	}else{
                return "<span class='label label-info'>权限</span>";
        	}
        }},
        {title: '状态', field: 'statusName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='启用'){
                return "<span class='label label-success'>启用</span>";
        	}else{
                return "<span class='label label-warning'>禁用</span>";
        	}
        }},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:Resource.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
    		_html += '<a href="javascript:Resource.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
    		return _html;
        }}];
    return columns;
};


/**
 * 检查是否选中
 */
Resource.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Resource.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加资源
 */
Resource.add = function () {
	this.layerIndex = Sys.open('新增资源',Sys.backendPath + '/resource_front/resource_add',['600px', '360px']);
};

/**
 * 点击修改
 */
Resource.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	this.layerIndex = Sys.open('修改资源',Sys.backendPath + '/resource_front/resource_edit/' + id,['600px', '360px']);
    }
};

/**
 * 删除
 */
Resource.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/resource_front/remove", function (data) {
	            Sys.success("删除成功!");
	            Resource.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("resourceId", id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除资源【" + name + "】?",operation);
    }
};

/**
 * 搜索
 */
Resource.search = function () {
    var queryData = {};

    queryData['resourceName'] = $("#resourceName").val();
    queryData['level'] = $("#level").val();

    Resource.table.refresh({query: queryData});
}
 
$(function () {
    var defaultColunms = Resource.initColumn();
    var table = new BSTreeTable(Resource.id, Sys.backendPath + "/resource_front/list", defaultColunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setParentIdField("parentId");
    table.setExpandAll(true);
    table.init();
    Resource.table = table;
});
